package com.aifeng.hassan_food.mine

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.aifeng.base.BaseActivity
import com.aifeng.common_ui.helper.*
import com.aifeng.hassan_food.BuildConfig
import com.aifeng.hassan_food.MainActivity
import com.aifeng.hassan_food.R
import com.aifeng.hassan_food.login.LoginActivity
import com.aifeng.utils.bindLifeCycle
import com.aifeng.utils.subscribeBy
import com.aifeng.utils.subscribeByNoError
import com.aifeng.viewmodel.SettingViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.activity_setting_phone.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class SettingPhoneActivity : BaseActivity() {

    private var mTelOld: String = ""
    private var mTelNew: String = ""
    private var mCodeOld: String = ""
    private var mCodeNew: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting_phone)
        initTitle(title = "修改手机号")
    }

    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        phone_old.setText(getString("phone"))
        phone_old.setSelection(phone_old.text.length)
        phone_old.isFocusable = false
    }

    override fun getViewModel() = getViewModel<SettingViewModel>()

    @SuppressLint("SetTextI18n")
    override fun doClick(v: View) {
        super.doClick(v)
        when (v.id) {
            R.id.bt_yzm01 -> {
                when {
                    phone_old.text.isBlank() -> {
                        phone_old.requestFocus()
                        toast("请输入原手机号")
                        return
                    }
                    !phone_old.text.isMobile() -> {
                        phone_old.requestFocus()
                        toast("请输入正确的原手机号")
                        return
                    }
                }

                getViewModel().getCode(phone_old.textString(), "3")
                    .bindLifeCycle(getLifecycleOwner())
                    .subscribeByNoError(
                        event = getViewModel(),
                        onSuccess = { inner ->
                            mTelOld = phone_old.textString()
                            mCodeOld = inner
                            @Suppress("ConstantConditionIf")
                            if (BuildConfig.LOG_DEBUG) {
                                phone_yzm01.setText(mCodeOld)
                                phone_yzm01.setSelection(phone_yzm01.text.length)
                            }

                            getViewModel().countDown(count = 60)
                                .bindLifeCycle(getLifecycleOwner())
                                .subscribeBy(
                                    onNext = {
                                        bt_yzm01.text = "${60 - it}s后重试"
                                        bt_yzm01.isClickable = false
                                    },
                                    onComplete = {
                                        bt_yzm01.text = "重新获取"
                                        bt_yzm01.isClickable = true
                                    }
                                )
                        }
                    )
            }
            R.id.bt_yzm02 -> {
                when {
                    phone_new.text.isBlank() -> {
                        phone_new.requestFocus()
                        toast("请输入新手机号")
                        return
                    }
                    !phone_new.text.isMobile() -> {
                        phone_new.requestFocus()
                        toast("请输入正确的新手机号")
                        return
                    }
                }

                getViewModel().getCode(phone_new.textString(), "3")
                    .bindLifeCycle(getLifecycleOwner())
                    .subscribeByNoError(
                        event = getViewModel(),
                        onSuccess = { inner ->
                            mTelNew = phone_new.textString()
                            mCodeNew = inner
                            @Suppress("ConstantConditionIf")
                            if (BuildConfig.LOG_DEBUG) {
                                phone_yzm02.setText(mCodeNew)
                                phone_yzm02.setSelection(phone_yzm02.text.length)
                            }

                            getViewModel().countDown(count = 60)
                                .bindLifeCycle(getLifecycleOwner())
                                .subscribeBy(
                                    onNext = {
                                        bt_yzm02.text = "${60 - it}s后重试"
                                        bt_yzm02.isClickable = false
                                    },
                                    onComplete = {
                                        bt_yzm02.text = "重新获取"
                                        bt_yzm02.isClickable = true
                                    }
                                )
                        }
                    )
            }
            R.id.bt_sure -> {
                when {
                    phone_old.text.isBlank() -> {
                        phone_old.requestFocus()
                        toast("请输入原手机号")
                        return
                    }
                    phone_yzm01.text.isBlank() -> {
                        phone_yzm01.requestFocus()
                        toast("请输入原手机验证码")
                        return
                    }
                    phone_new.text.isBlank() -> {
                        phone_new.requestFocus()
                        toast("请输入新手机号")
                        return
                    }
                    phone_yzm02.text.isBlank() -> {
                        phone_yzm02.requestFocus()
                        toast("请输入新手机验证码")
                        return
                    }
                    !phone_old.text.isMobile() -> {
                        phone_old.requestFocus()
                        toast("请输入正确的原手机号")
                        return
                    }
                    mCodeOld.isEmpty() -> {
                        toast("请点击获取原手机验证码")
                        return
                    }
                    phone_old.textString() != mTelOld -> {
                        toast("原手机号码不匹配，请重新获取验证码")
                        return
                    }
                    phone_yzm01.textString() != mCodeOld -> {
                        phone_yzm01.requestFocus()
                        phone_yzm01.setText("")
                        toast("请输入正确的原手机验证码")
                        return
                    }
                    !phone_new.text.isMobile() -> {
                        phone_new.requestFocus()
                        toast("请输入正确的新手机号")
                        return
                    }
                    mCodeNew.isEmpty() -> {
                        toast("请点击获取新手机验证码")
                        return
                    }
                    phone_new.textString() != mTelNew -> {
                        toast("新手机号码不匹配，请重新获取验证码")
                        return
                    }
                    phone_yzm02.textString() != mCodeNew -> {
                        phone_yzm02.requestFocus()
                        phone_yzm02.setText("")
                        toast("请输入正确的新手机验证码")
                        return
                    }
                }

                getViewModel().editPhone(
                    "userid" to getString("userId"),
                    "phone" to phone_old.textString(),
                    "code" to phone_yzm01.textString(),
                    "newphone" to phone_new.textString(),
                    "newcode" to phone_yzm02.textString()
                )
                    .bindLifeCycle(getLifecycleOwner())
                    .subscribeByNoError(
                        event = getViewModel(),
                        onSuccess = {
                            toast("手机号码修改成功！")
                            putShared("phone", phone_new.textString())

                            clearShared(
                                "userId",
                                "nickName",
                                "shopName",
                                "headImg",
                                "shopId",
                                "balance",
                                "openId",
                                "realName",
                                "bankCard",
                                "bindId",
                                "bindImg",
                                "bindName",
                                "createData"
                            )
                            isLogin = false

                            LiveEventBus.get("loginStatus").post("quit")
                            LiveEventBus.get("mainLogin").post("quit")
                            ActivityStack.screenManager.popAllActivityExceptOne(MainActivity::class.java)

                            startActivity<LoginActivity>()
                        }
                    )
            }
        }
    }

}
